package com.test.ipc.pipe;

import com.test.ipc.Logger;

import java.io.*;

public class Parent {
    private static final String TAG = "Pipe.Parent";
    private static OutputStream mOutputStream = null;
    private static InputStream mInputStream = null;

    public static void main(String[] args) {
        Process mProcess = null;

        try {
            mProcess = Runtime.getRuntime().exec("java com.test.ipc.pipe.Child");
        } catch (Exception e) {
            e.printStackTrace();
            Logger.e(TAG+","+e.getMessage());
        }
        if (mProcess == null) {
            Logger.e(TAG+",mProcess == null");
            return;
        }

        mOutputStream = mProcess.getOutputStream();
        mInputStream = mProcess.getInputStream();

        Thread mWriteThread = new Thread(new Runnable() {
            @Override
            public void run() {
                int i=0;
                while (true) {
                    i++;
                    if (i>=5 || i<0) {
                        try {
                            mOutputStream.flush();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        continue;
                    }
                    try {
                        mOutputStream.write((""+i+"\r\n").getBytes());
                        mOutputStream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                        Logger.e(TAG+","+e.getMessage());
                        break;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        mWriteThread.start();

        Thread mReadThread = new Thread(new Runnable() {
            @Override
            public void run() {
                BufferedReader bfr = new BufferedReader(new InputStreamReader(mInputStream));
                while (true) {
                    try {
                        String line = bfr.readLine();
                        if (line != null) {
                            Logger.d(TAG+",Rcv: " + line);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Logger.e(TAG+","+e.getMessage());
                        break;
                    }
                }
            }
        });
        mReadThread.start();

        try {
            mWriteThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            mReadThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            mInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
